﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Codes {
    public class h {
        public static string getJson(System.Data.DataTable dt) {
            System.Text.StringBuilder jstr=new System.Text.StringBuilder();
            if(dt.Rows.Count>0) {
                jstr.Append("[");
                for(int i = 0;i < dt.Rows.Count;i++) {
                    jstr.Append("{");
                    for(int j = 0;j < dt.Columns.Count;j++) {
                        jstr.Append(string.Format("\"{0}\":\"{1}\",", dt.Columns[j].ColumnName, dt.Rows[i][j].ToString()));
                    }
                    if(jstr.Length>2) {
                        jstr.Remove(jstr.Length-1, 1);
                    }
                    jstr.Append("},");
                }
                jstr.Remove(jstr.Length-1, 1);
                jstr.Append("]");
            }
            return jstr.ToString();
        }

        public static string getJson(System.Data.DataSet ds) {
            System.Data.DataTable dt=ds.Tables[0];
            System.Text.StringBuilder jstr=new System.Text.StringBuilder();
            if(dt.Rows.Count>0) {
                jstr.Append("[");
                for(int i = 0;i < dt.Rows.Count;i++) {
                    jstr.Append("{");
                    for(int j = 0;j < dt.Columns.Count;j++) {
                        jstr.Append(string.Format("\"{0}\":\"{1}\",", dt.Columns[j].ColumnName, dt.Rows[i][j].ToString()));
                    }
                    if(jstr.Length>2) {
                        jstr.Remove(jstr.Length-1, 1);
                    }
                    jstr.Append("},");
                }
                jstr.Remove(jstr.Length-1, 1);
                jstr.Append("]");
            }
            return jstr.ToString();
        }
        public static string GetTimeStamp() {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
        public static Hashtable RequestConvertToHashtable(HttpRequest r, bool filterFirstKey=true) {
            Hashtable ht=new Hashtable();
            for(int i = (filterFirstKey?1:0);i < r.Form.Count;i++) {
                ht.Add(r.Form.Keys[i], r[r.Form.Keys[i]]);
            }
            return ht;
        }
        public static Hashtable RequestConvertToHashtable(HttpRequest r, string[] filterKey) {
            Hashtable ht=new Hashtable();
            for(int i = 0;i < r.Form.Count;i++) {
                if(!((IList)filterKey).Contains(r.Form.Keys[i].ToString())) {
                    ht.Add(r.Form.Keys[i], r[r.Form.Keys[i]]);
                }
            }
            return ht;
        }
        public static string ManuTime(string date) {
            DateTime dtBegin = DateTime.Parse(DateTime.Parse(date).ToString("yyyy-MM-dd HH:mm"));
            DateTime dtNow = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
            TimeSpan ts = dtNow.Subtract(dtBegin);
            int day = (int)ts.TotalDays;
            int hour = (int)ts.TotalHours;
            int min = (int)ts.TotalMinutes;
            if(day == 0) {
                if(min <= 0) {
                    return "刚刚";
                }
                if((min < 0 ? min * -1 : min) <= 60) {
                    return (min < 0 ? min * -1 : min) + "分钟前";
                }
                if(hour >= 1) {
                    return hour + "小时前";
                }
            }
            if(day == 1) {
                return "昨天 " + dtBegin.ToString("HH:mm");
            }
            if(day == 2) {
                return "2天前 " + dtBegin.ToString("HH:mm");
            }
            if(dtBegin.Year == dtNow.Year) {
                return dtBegin.ToString("MM月dd日 HH:mm");
            }
            return dtBegin.ToString("yyyy年MM月dd日 HH:mm");
        }
    }
}